package com.lsz.deport.ui.login;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.common.CountDownUtlis;
import com.common.config.Global;
import com.common.mvp.BaseMvpActivity;
import com.common.util.PhoneNumTools;
import com.common.widght.EditTextWithDel;
import com.lsz.deport.R;
import com.lsz.deport.json.UserEntity;
import com.lsz.deport.ui.MainActivity;
import com.util_code.dialog.LoadingDialog;
import com.util_code.utils.ToastUtils;

import java.util.Objects;

import butterknife.BindView;

public class LoginActivity extends BaseMvpActivity<LoginView, LoginPresent> implements LoginView, View.OnClickListener {

    @BindView(R.id.et_login_user)
    EditTextWithDel etLoginUser;
    @BindView(R.id.et_login_password)
    EditTextWithDel etLoginPassword;
    @BindView(R.id.tv_get_verifi_code)
    TextView tvGetVerifiCode;
    @BindView(R.id.tv_yellow_button)
    TextView tvYellowButton;

    private CountDownUtlis countDownUtlis;
    private LoadingDialog loadingDialog;

    public static void startActivity(Activity activity) {
        Intent intent = new Intent(activity, LoginActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void setTranslateStatusBar(boolean barColor) {
        super.setTranslateStatusBar(barColor);
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void setupView() {
        tvYellowButton.setOnClickListener(this::onClick);
        tvGetVerifiCode.setOnClickListener(this::onClick);

        etLoginUser.addTextChangedListener(new LoginTextWatcher(0));
        etLoginPassword.addTextChangedListener(new LoginTextWatcher(1));

        if (loadingDialog == null) {
            loadingDialog = new LoadingDialog();
        }
    }

    @Override
    protected void setupData(Bundle savedInstanceState) {
        UserEntity userBean = Global.getUserBean();
        if (userBean != null && userBean.tokenInfo != null && !TextUtils.isEmpty(userBean.tokenInfo.access_token)) {
            MainActivity.startActivity(this);
        } else {
            countDownUtlis = new CountDownUtlis(this);
        }
    }

    class LoginTextWatcher implements TextWatcher {

        int index = 0;

        LoginTextWatcher(int index) {
            this.index = index;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (index == 0) {

            } else if (index == 1) {

            }
            String phone = Objects.requireNonNull(etLoginUser.getText()).toString();
            String captcha = Objects.requireNonNull(etLoginPassword.getText()).toString();
            if (!TextUtils.isEmpty(phone) && phone.length() == 11 && !TextUtils.isEmpty(captcha) && captcha.length() == 6) {
                tvYellowButton.setEnabled(true);
            } else {
                tvYellowButton.setEnabled(false);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_get_verifi_code:
                String phoneNumber = Objects.requireNonNull(etLoginUser.getText()).toString();
                boolean picCode = setPicCode(phoneNumber);
                if (picCode) {
                    countDownUtlis.countDown(60, countDownListener);
                }
                break;
            case R.id.tv_yellow_button:
                String phone = Objects.requireNonNull(etLoginUser.getText()).toString();
                String captcha = Objects.requireNonNull(etLoginPassword.getText()).toString();
                boolean picCode1 = setPicCode(phone);
                if (!picCode1) {
                } else if (TextUtils.isEmpty(captcha) || captcha.length() < 6) {
                    ToastUtils.showLongToastSafe("请输入正确的验证码");
                    return;
                }
                if (!loadingDialog.isVisible()) {
                    loadingDialog.show(getSupportFragmentManager(), "loadingdialog");
                }
                getPresenter().postLogin(phone, captcha);
                break;

            default:
                break;
        }
    }

    @Override
    public <T> void onSuccess(T t) {
        loadingDialog.dismissAllowingStateLoss();
        MainActivity.startActivity(this);
    }

    @Override
    public void onFali() {
        loadingDialog.dismissAllowingStateLoss();
    }

    private CountDownUtlis.CountDownListener countDownListener = new CountDownUtlis.CountDownListener() {
        @Override
        public void onCountStart() {
            String phone = Objects.requireNonNull(etLoginUser.getText()).toString().replace(" ", "");
            getPresenter().getCaptcha(phone);
            String again = "重新发送( 60 )";
            tvGetVerifiCode.setText(again);
            tvGetVerifiCode.setTextColor(getResources().getColor(R.color.c_C9C9C9));
            tvGetVerifiCode.setEnabled(false);
        }

        @Override
        public void onCountProgress(long progress) {
            String again = "重新发送( " + progress + " )";
            tvGetVerifiCode.setText(again);
            tvGetVerifiCode.setTextColor(getResources().getColor(R.color.c_C9C9C9));
            tvGetVerifiCode.setEnabled(false);
        }

        @Override
        public void onCountEnd() {
            String getVerification = "获取验证码";
            tvGetVerifiCode.setText(getVerification);
            tvGetVerifiCode.setTextColor(getResources().getColor(R.color.c_FABE00));
            tvGetVerifiCode.setEnabled(true);
        }

        @Override
        public void onCountError() {
            String getVerification = "获取验证码";
            tvGetVerifiCode.setText(getVerification);
            tvGetVerifiCode.setTextColor(getResources().getColor(R.color.c_FABE00));
            tvGetVerifiCode.setEnabled(true);
        }
    };

    public boolean setPicCode(String phone) {
        boolean checkMobile = PhoneNumTools.checkMobile(phone.replace(" ", ""));
        if (!checkMobile) {
            ToastUtils.showShortToast("请输入正确的手机号");
        }
        return checkMobile;
    }
}
